package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.TradeArea;
import com.asm.repository.TradeAreaRepository;
import com.asm.util.SnowflakeIdWorker;
import com.asm.vo.sys.DistrictVo;
import com.asm.vo.sys.TradeAreaVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author morningbear
 */
@Service
public class TradeAreaService {

    @Resource
    private TradeAreaRepository tradeAreaRepository;

    @Resource
    private DistrictService districtService;

    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取商圈
     *
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @param page       分页
     * @return 商圈
     */
    private PageResult<TradeAreaVo> getTradeAreaVoResult(Integer pageNumber, Integer pageSize, Page<TradeArea> page) {
        List<TradeAreaVo> tradeAreaVoList = new ArrayList<>();
        for (TradeArea tradeArea : page.getContent()) {
            TradeAreaVo tradeAreaVo = new TradeAreaVo();
            BeanUtils.copyProperties(tradeArea, tradeAreaVo);
            DistrictVo districtVo = districtService.findDistrictById(tradeAreaVo.getPrefecture());
            tradeAreaVo.setDistrict(districtVo.getProvince() + districtVo.getCity() + districtVo.getDistrict());
            tradeAreaVoList.add(tradeAreaVo);
        }
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), tradeAreaVoList);
    }

    /**
     * 查询所有商圈
     *
     * @return 商圈
     */
    public PageResult<TradeAreaVo> findAllTradeAreas(Integer pageNumber, Integer pageSize) {
        Page<TradeArea> page = tradeAreaRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return getTradeAreaVoResult(pageNumber, pageSize, page);
    }

    /**
     * 添加商圈
     *
     * @param tradeAreaVo 商圈
     * @return 商圈编号
     */
    @Transactional(rollbackFor = Exception.class)
    public Long addTradeArea(TradeAreaVo tradeAreaVo) {
        TradeArea tradeArea = new TradeArea();
        BeanUtils.copyProperties(tradeAreaVo, tradeArea);
        tradeArea.setId(snowflakeIdWorker.nextId());
        tradeArea.setCreateAt(new Date());
        tradeArea.setUpdateAt(new Date());
        tradeArea.setCreateBy(1L);
        tradeArea.setUpdateBy(1L);
        tradeAreaRepository.save(tradeArea);
        return tradeArea.getId();
    }

    /**
     * 删除小区
     *
     * @param id 商圈编号
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteTradeArea(Long id) {
        TradeArea tradeArea = tradeAreaRepository.findById(id).get();
        tradeArea.setIsDel(true);
        tradeArea.setUpdateBy(1L);
        tradeArea.setUpdateAt(new Date());
        tradeAreaRepository.save(tradeArea);
    }

    /**
     * 更新小区
     *
     * @param tradeAreaVo 商圈
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateTradeArea(TradeAreaVo tradeAreaVo) {
        TradeArea tradeArea = tradeAreaRepository.findById(tradeAreaVo.getId()).get();
        BeanUtils.copyProperties(tradeAreaVo, tradeArea);
        tradeArea.setUpdateBy(1L);
        tradeArea.setUpdateAt(new Date());
        tradeAreaRepository.save(tradeArea);
    }

}
